<template>
  <el-row :gutter="20" style="margin-bottom: 10px;">
      <el-col :span="6" >
          <el-button size="large"  
              @click="saveDialogFormVisible = true" >新增菜品</el-button>
      </el-col>      
  </el-row>

  <el-row style="margin:0 20px;">
    <el-table :data="dishes" stripe style="width: 100%" highlight-current-row >
      <el-table-column prop="dishId" label="菜品编号" width="100" />
      <el-table-column prop="dishName" label="菜品名称" width="180"/>
      <el-table-column label="菜品图片" width="180">
        <template #default="scope">
        <div style="display: flex; align-items: center">
          <el-image
                :src="'http://localhost:8081/'+scope.row.imageName" 
                class="image" width="100%" fit="contain" />
        </div>
        </template>
      </el-table-column>

      <el-table-column prop="price" label="菜品价格" width="180" />
      <el-table-column prop="categoryId" label="分类名称" width="180" :formatter="categoryFormat"/>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button
          size="small"
          type="danger"
          @click="showUpdateDialog(scope.row)"
          >
          <el-text  style="color: #fff;" >修改价格</el-text>
          </el-button>
        <el-button
          size="small"
          type="danger"
          @click="swithState(scope.$index, scope.row)"
          >
          <el-text  style="color: #fff;">停用</el-text>
          </el-button>
        </template>

      </el-table-column>
    </el-table>
  </el-row>

  <el-dialog v-model="saveDialogFormVisible" title="新增菜品" width="500">
    <el-form :model="form" :rules="rules">
      <el-form-item label="菜品名称" prop="dishName" >
        <el-input v-model="form.dishName" autocomplete="off" />
      </el-form-item>
      <el-form-item label="菜品价格" prop="price">
        <el-input v-model="form.price" autocomplete="off" />
      </el-form-item>
      <el-select v-model="form.categoryId" placeholder="选择菜品分类" prop="categoryId">
        <el-option v-for="category in categories" 
          :key="category.categoryId" :label="category.categoryName" :value="category.categoryId" />
      </el-select>
      <el-form-item label="菜品图片" style="margin-top: 20px;" prop="imageUrl">
        <el-upload
        class="avatar-uploader"
        action="/api/admin/uploadDishImage"  
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
      >
        <img v-if="imageUrl" :src="imageUrl" class="avatar" />
        <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
      </el-upload>
      </el-form-item>
      
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="saveDialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="saveDish">
          确定
        </el-button>
      </div>
    </template>
  </el-dialog>

  <el-dialog v-model="updateDialogFormVisible" title="修改价格" width="500">
    <el-form :model="form">
      <el-form-item label="菜品名称" >
        <el-input v-model="currentDish.dishName" autocomplete="off" disabled />
      </el-form-item>

      <el-form-item label="菜品价格" >
        <el-input v-model="currentDish.price" autocomplete="off" />
      </el-form-item>
      
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="updateDialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="updateDish">
          确定
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>



<script  setup>

import { getCurrentInstance,ref,onBeforeMount,reactive } from "vue"
import { useStore } from '@/stores'
const store = useStore()

const { proxy } = getCurrentInstance()
let saveDialogFormVisible = ref(false)
let updateDialogFormVisible = ref(false)
let imageUrl = ref('')
const dishes = ref([])

let currentDish=ref()
let categories=ref()

const rules = reactive({
  dishName: [
    { required: true, message: '请输入菜品名称', trigger: 'blur' },
    
  ],
  categoryId: [
    {
      required: true,
      message: '请选择菜品分类',
      trigger: 'change',
    },
  ],
  price: [
    {
      required: true,
      type:'number',
      message: '请输入菜品价格',
      trigger: 'blur',
    },
    { min: 1,  message: '菜品价格必须为正整数', trigger: 'blur' },
  ],
  imageUrl: [
    {
      required: true,
      message: '请选择菜品图片',
      trigger: 'blur',
    },
  ]
})
const handleAvatarSuccess=(res,file) => {
  console.log(res)
  form.imageName=res.data
  imageUrl.value=URL.createObjectURL(file.raw)
}

function categoryFormat(row, column, cellValue, index){
  return categories.value.find(item=>item.categoryId==cellValue).categoryName
}


onBeforeMount(()=>{
  getCategories()
  getDishes()  
})

async function getCategories(){
  let { data: res} = await proxy.$http({
    method:'get',
    headers: {
      'Authorization': store.token	//	可以是授权凭证用的参数值
    },
    url:'/api/category'
	})
  console.log(res.data)
  categories.value=res.data
}

async function getDishes(){
  let { data: res} = await proxy.$http({
    method:'get',
    headers: {
      'Authorization': store.token	//	可以是授权凭证用的参数值
      },
    url:'/api/admin/dish'
	})
  console.log(res.data)
  dishes.value=res.data
}

const handleEdit = (index, row) => {
  console.log(index, row)
}

function showUpdateDialog(row){
  updateDialogFormVisible.value=true
  currentDish.value=row
}

const form = reactive({
  dishName: '',
  price:0,
  imageName:'',
  categoryId:''
})

async function updateDish(){
  let { data: res} = await proxy.$http({
    method:'put',
    headers: {
      'Authorization': store.token	//	可以是授权凭证用的参数值
    },
    data:currentDish.value,
    url:'/api/admin/dish'
	})
  updateDialogFormVisible.value = false
  getDishes()
}

async function saveDish(){
  let { data: res} = await proxy.$http({
    method:'post',
    headers: {
      'Authorization': store.token	//	可以是授权凭证用的参数值
    },
    data:form,
    url:'/api/admin/dish'
	})
  saveDialogFormVisible.value = false
  getDishes()
}

</script>

<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>